package com.acwing.partition1;

import java.io.*;

/**
 * @author `RKC`
 * @date 2021/11/26 10:05
 */
public class AC2零一背包问题 {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] s = reader.readLine().split(" ");
        int n = Integer.parseInt(s[0]), m = Integer.parseInt(s[1]);
        int[] weight = new int[n], value = new int[n];
        for (int i = 0; i < n; i++) {
            s = reader.readLine().split(" ");
            int a = Integer.parseInt(s[0]), b = Integer.parseInt(s[1]);
            weight[i] = a;
            value[i] = b;
        }
        int ans = dynamicProgramming0(weight, value, m);
        writer.write(ans + "\n");
        writer.flush();
    }

    private static int dynamicProgramming0(int[] weight, int[] value, int m) {
        int n = weight.length;
        int[] dp = new int[m + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = m; j >= weight[i - 1]; j--) {
                dp[j] = Math.max(dp[j], dp[j - weight[i - 1]] + value[i - 1]);
            }
        }
        return dp[m];
    }

    private static int dynamicProgramming(int[] weight, int[] value, int m) {
        int n = weight.length;
        int[][] dp = new int[n + 1][m + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                dp[i][j] = dp[i - 1][j];
                if (j >= weight[i - 1]) {
                    dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - weight[i - 1]] + value[i - 1]);
                }
            }
        }
        return dp[n][m];
    }
}
